package com.slf.engine.services.impl;

import java.rmi.RemoteException;

import org.apache.log4j.Logger;
import org.apache.log4j.Priority;

import com.slf.common.client.control.WSController;
import com.slf.common.client.control.WSControllerServiceLocator;
import com.slf.common.client.login.WSLoginProxy;
import com.slf.common.client.login.WSLoginProxyServiceLocator;
import com.slf.engine.bo.Order;
import com.slf.engine.common.SysContants;
import com.slf.engine.common.SysContext;
import com.slf.engine.services.IHpBossService;
import com.slf.engine.services.helper.ServiceHelper;
import com.zhkj.wsaction.exception.ActionException;
import com.zhkj.wsaction.exception.ServiceException;
import com.zhkj.wsaction.type.TPacket;
import com.zhkj.wsaction.type.TPacketImpl;

public class HpbossServiceImpl implements IHpBossService {

	private static final Logger log = Logger.getLogger(HpbossServiceImpl.class);
	private WSController controller;
	
	/**
	 * 登录BOSS系统
	 * @throws ServiceException 
	 * @throws RemoteException 
	 */
	private boolean doLogin()
	{
		try {
			WSLoginProxy loginProxy = new WSLoginProxyServiceLocator().getWSLogin();
			SysContext.BOSS_SESSION_TOKEN = loginProxy.login(SysContants.BOSS_LOGIN_NAME, SysContants.BOSS_LOGIN_PWD);
			return true;
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			log.log(Priority.ERROR,"登录BOSS系统出现请求错误");
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e1) {
				// TODO Auto-generated catch block
			}
			return doLogin();
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			log.log(Priority.ERROR,"登录BOSS系统出现请求错误");
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e1) {
				// TODO Auto-generated catch block
			}
			return doLogin();
		} catch (javax.xml.rpc.ServiceException e) {
			// TODO Auto-generated catch block
			log.log(Priority.ERROR,"登录BOSS系统出现请求错误");
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e1) {
				// TODO Auto-generated catch block
			}
			return doLogin();
		}
	}
	
	/**
	 * 获取一个订单
	 * @return
	 */
	public Order getOneOrder()
	{
		log.log(Priority.INFO, "获取订单");
		if(SysContext.BOSS_SESSION_TOKEN == null || "".equals(SysContext.BOSS_SESSION_TOKEN))
		{
			doLogin();
		}
		try {
			controller = new WSControllerServiceLocator().getWSController();
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			log.log(Priority.ERROR,"BOSS业务网关出现问题。。无法建立连接");
			return null;
		} catch (javax.xml.rpc.ServiceException e) {
			// TODO Auto-generated catch block
			log.log(Priority.ERROR,"BOSS业务网关出现问题。。无法建立连接");
			return getOneOrder();
		}
		TPacket packet = new TPacketImpl();
		packet.setCode(SysContants.REQ_CODE);
		packet.setAuthinfo(SysContext.BOSS_SESSION_TOKEN);
		packet.setAction("request");
		packet.writeToVarData("TDBH", SysContants.TDBH);
		packet.writeToVarData("SSDQ", "330100");
		//提交业务请求
		String respStr = "";
		try {
			respStr = controller.execute(packet.saveToXml());
		} catch (ActionException e) {
			// TODO Auto-generated catch block
			log.log(Priority.ERROR,"业务解析出现错误【" + respStr + "】");
			return null;
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			log.log(Priority.ERROR,"BOSS业务网关出现问题。。无法建立连接");
			return null;
		}
		packet.loadFromXml(respStr);
		
		//超时。。重新登录
		if(Integer.parseInt(packet.getCode()) < 0 && (packet.getCode().endsWith("302") || packet.getCode().endsWith("303") || packet.getCode().endsWith("304")))
		{
			doLogin();
			return getOneOrder();
		}
		Order order = null;
		if(packet.readFromVarDataAsStr("CZID") != null && !"0".equals(packet.readFromVarDataAsStr("CZID")))
		{
			//解析业务对象
			try {
				order = ServiceHelper.putPacketToOrder(packet);
				return order;
			} catch (Exception e) {
				// TODO Auto-generated catch block
				order.setClbz(SysContants.CZ_FAILURE);
				order.setCljg(e.getMessage());
				doBack(order);
			}
		}
		return null;
	}
	
	
	public boolean doBack(Order order)
	{
		try {
			controller = new WSControllerServiceLocator().getWSController();
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			log.log(Priority.ERROR,"BOSS业务网关出现问题。。无法建立连接");
			return false;
		} catch (javax.xml.rpc.ServiceException e) {
			// TODO Auto-generated catch block
			log.log(Priority.ERROR,"BOSS业务网关出现问题。。无法建立连接");
			return doBack(order);
		}
		log.log(Priority.INFO, "返回订单信息");
		TPacket packet = new TPacketImpl();
		packet.setAction("requset");
		packet.setAuthinfo(SysContext.BOSS_SESSION_TOKEN);
		packet.setCode(SysContants.RES_CODE);
		packet.writeToVarData("CZID", order.getOrderId());
		packet.writeToVarData("CLBZ", String.valueOf(order.getClbz()));
		packet.writeToVarData("CLJG", order.getCljg());
		packet.writeToVarData("YYSLSH", order.getYyslsh());
		packet.writeToVarData("SJJE",String.valueOf((int)Double.parseDouble(order.getMoney())*100));
		packet.writeToVarData("YHXM", "0");
		packet.writeToVarData("YHYE", "0");
		String repStr = "";
		try {
			repStr = controller.execute(packet.saveToXml());
			packet.loadFromXml(repStr);
			
			if(Integer.parseInt(packet.getCode()) < 0 && (packet.getCode().endsWith("302") || packet.getCode().endsWith("303") || packet.getCode().endsWith("304")))
			{
				doLogin();
				return doBack(order);
			}
			SysContext.IS_START = true;
			return true;
		} catch (ActionException e) {
			// TODO Auto-generated catch block
			log.log(Priority.ERROR,"业务处理结束返回BOSS出现问题【" + repStr + "】");
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			log.log(Priority.ERROR,"业务处理结束返回BOSS出现问题【" + repStr + "】");
		}
		return false;
	}
	
	public static void main(String args[])
	{
		
		new HpbossServiceImpl().getOneOrder();
	}
}
